package com.yang.crawler.file;


import org.springframework.stereotype.Component;

import java.io.*;
import java.util.HashSet;
import java.util.Set;

@Component
public class LocalFileUploader implements IFileUploader {
    private static final String PRE_URL = "http://localhost:80/upload/";

    private static final String PRE_DIRECTORY = "D:\\upload";

    private static final Set<String> filterCharacter = new HashSet<>();

    static {
        filterCharacter.add("|");
        filterCharacter.add(":");
        filterCharacter.add("：");
        filterCharacter.add("?");
        filterCharacter.add("？");
    }

    @Override
    public String uploadFile(String directory, String filename, InputStream inputStream) {
        // 处理一些特殊字符
        directory = filterSpecialCharacter(directory);
        filename = filterSpecialCharacter(filename);

        String directoryFilename = PRE_DIRECTORY + File.separator + directory;
        File file = new File(directoryFilename);
        if (!file.exists()) {
            file.mkdirs();
        }

        String absoluteFilename = directoryFilename + File.separator + filename;
        try (FileOutputStream fileOutputStream = new FileOutputStream(absoluteFilename)){
            byte[] buffer = new byte[8192];
            int bytesRead = -1;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return PRE_URL + directory.replace(File.separator, "/") + "/" + filename;
    }

    @Override
    public InputStream downloadFile(String directory, String filename) throws FileNotFoundException {
        // 处理一些特殊字符
        directory = filterSpecialCharacter(directory);
        filename = filterSpecialCharacter(filename);
        String directoryFilename = PRE_DIRECTORY + File.separator + directory;
        String absoluteFilename = directoryFilename + File.separator + filename;
        return new FileInputStream(absoluteFilename);
    }

    private String filterSpecialCharacter(String str) {
        for (String s : filterCharacter) {
            str = str.replace(s, "");
        }
        return str;
    }

    @Override
    public InputStream downloadFile(String url) throws FileNotFoundException {
        //http://localhost:80/img/zq99299.github.io/DDD 实战课  NOTE-BOOK2/开篇  学好了 DDD，你能做什么  NOTE-BOOK2/dc32e8e4a317fe00121ce18adc407c66dc32e8e4.jpg
        String suffixPath = url.substring(PRE_URL.length());
        suffixPath = suffixPath.replace("/", File.separator);
        String filePath = PRE_DIRECTORY + File.separator + suffixPath;
        return new FileInputStream(filePath);
    }

    @Override
    public boolean isFileExist(String directory, String filename) {
        // 处理一些特殊字符
        directory = filterSpecialCharacter(directory);
        filename = filterSpecialCharacter(filename);

        String directoryFilename = PRE_DIRECTORY + File.separator + directory;
        File dir = new File(directoryFilename);
        if (!dir.exists()) {
            return false;
        }

        String absoluteFilename = directoryFilename + File.separator + filename;
        File file = new File(absoluteFilename);
        return file.exists();
    }
}
